package com.vitabuybuy.tv.ui.mine.account

import android.os.Bundle
import android.os.CountDownTimer
import android.view.View
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.dto.MessageEvent
import com.library.common.ext.mmkv
import com.library.common.ext.setOnclickNoRepeat

import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityThirdPartyBindBinding
import com.library.common.toast.XToast
import com.vitabuybuy.tv.ui.mine.account.viewmodel.SmsVerifyViewModel
import com.vitabuybuy.tv.utils.EventCode
import com.library.common.util.MMKVContancts
import com.vitabuybuy.tv.dialog.AreaCodePopup
import kotlinx.android.synthetic.main.activity_find_password.*
import kotlinx.android.synthetic.main.activity_third_party_bind.*
import kotlinx.android.synthetic.main.activity_third_party_bind.countryCodePicker
import kotlinx.android.synthetic.main.activity_third_party_bind.etPhone
import kotlinx.android.synthetic.main.activity_third_party_bind.etSmsCode
import kotlinx.android.synthetic.main.activity_third_party_bind.tvGetSmsCode
import org.greenrobot.eventbus.EventBus

class ThirdPartyBindActivity : BaseActivity<SmsVerifyViewModel, ActivityThirdPartyBindBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_third_party_bind

    private var from :Int?= FROM_BIND_NEW_PHONE
    private var isGetCode = false
    private var defaultAreaCode = "86"

    override fun onGetBundle(bundle: Bundle?) {
        from = bundle?.getInt("from")
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(
            when (from) {
                FROM_BIND_NEW_PHONE -> getString(R.string.bind_phone)
                FROM_SMSVERIFY -> getString(R.string.bind_new_phone)
                else -> ""
            }
        )
    }

    override fun eventHandling() {
        setOnclickNoRepeat(tvGetSmsCode, btnFinish){v->
            if (etPhone.text.isEmpty()) {
                XToast.normal(getString(R.string.enter_phone_number))
                return@setOnclickNoRepeat
            }
            if (mmkv.decodeBool(MMKVContancts.IS_LOGIN_IN, false)) {
                if (etPhone.text.toString() == mmkv.decodeString(MMKVContancts.USER_PHONE, "")) {
                    XToast.normal(getString(R.string.pls_user_different_phone))
                    return@setOnclickNoRepeat
                }
            }
            when (v) {
                tvGetSmsCode -> {
                    viewModel.sendToPhoneV2(
                        etPhone.text.toString(),
                        defaultAreaCode
                    )
                }
                btnFinish -> {
                    when {
                        !isGetCode -> XToast.normal(getString(R.string.please_get_code))
                        etSmsCode.text.isEmpty() -> XToast.normal(getString(R.string.enter_code))
                        else -> viewModel.validateByPhone(
                            etPhone.text.toString(),
                            etSmsCode.text.toString(),
                            defaultAreaCode
                        )
                    }
                }
            }
        }
        countryCodePicker.setOnClickListener{
            AreaCodePopup(this){
                countryCodePicker.text = it
                defaultAreaCode = it.replace("+","")
            }.showPopupWindow()
        }
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            1 -> {
                XToast.success(getString(R.string.already_sms_code))
                timer.start()
            }
            2 -> {
                viewModel.resetPhone(etPhone.text.toString(), defaultAreaCode)
            }
            3 -> {
                XToast.success(getString(R.string.pls_user_new_phone_login))
                EventBus.getDefault()
                    .post(MessageEvent(EventCode.login_out, getString(R.string.logout)))
            }
        }
    }

    private val timer = object : CountDownTimer((60 * 1000).toLong(), 1000) {
        override fun onTick(millisUntilFinished: Long) {
            isGetCode = true
            mBinding.tvGetSmsCode.text = String.format(
                getString(R.string.get_sms_code_s),
                millisUntilFinished / 1000
            )
            mBinding.tvGetSmsCode.isEnabled = false
        }

        override fun onFinish() {
            mBinding.tvGetSmsCode.text = getString(R.string.get_sms_code)
            mBinding.tvGetSmsCode.isEnabled = true
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        timer.cancel()
    }


    companion object {
        const val FROM_SMSVERIFY = 1001
        const val FROM_BIND_NEW_PHONE = 1002
        fun openActivity(mContext: BaseActivity<*, *>, bundle: Bundle) {
            mContext.startActivity(bundle, ThirdPartyBindActivity::class.java)
        }
    }
}
